package com.ashin.topic;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.TimeUnit;

/**
 * @Author: Ashin
 * @Date: 2021/4/26
 * @Description: com.ashin
 * @Version: 1.0.0
 */
public class SyncProducer {

    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("zjh6");
        // Specify name server addresses.
        producer.setNamesrvAddr("192.168.117.130:9876");
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 5; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("TopicTest1", "TagA", ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));

            //Call send message to deliver message to one of brokers.
            SendResult sendResult = producer.send(msg);

            System.out.printf("%s%n", sendResult);
        }

        //Shut down once the producer instance is not longer in use.
        TimeUnit.SECONDS.sleep(1000);
        producer.shutdown();
    }
}
